 #include "map.h"

  void loadMap(char *name)
 {
    int x, y;
    FILE *fp;

    fp = fopen(name, "rb");

    

    if (fp == NULL)
    {
        printf("Failed to open map %s\n", name);
        exit(1);
    }


    

    map.maxX = map.maxY = 0;

    for (y = 0; y < MAX_MAP_Y; y++)
    {
        for (x = 0; x < MAX_MAP_X; x++)
        {
            

            fscanf(fp, "%d", &map.tile[y][x]);

            

            if (map.tile[y][x] > 0)
            {
                if (x > map.maxX)
                {
                    map.maxX = x;
                }

                if (y > map.maxY)
                {
                    map.maxY = y;
                }
            }
        }
    }



    map.maxX = (map.maxX + 1) * TILE_SIZE;
    map.maxY = (map.maxY + 1) * TILE_SIZE;



    map.startX = map.startY = 0;



    fclose(fp);

  }




   void drawMap(int maxW, int maxH)
 {
    int x, y, mapX, x1, x2, mapY, y1, y2, xsource, ysource, a;

   
    mapX = map.startX / TILE_SIZE;

    
    x1 = (map.startX % TILE_SIZE) * -1;

   
    x2 = x1 + maxW + (x1 == 0 ? 0 : TILE_SIZE);

    
    mapY = map.startY / TILE_SIZE;
    y1 = (map.startY % TILE_SIZE) * -1;
    y2 = y1 + maxH + (y1 == 0 ? 0 : TILE_SIZE);

   
    

    for (y = y1; y < y2; y += TILE_SIZE)
    {
        

        mapX = map.startX / TILE_SIZE;

        

        for (x = x1; x < x2; x += TILE_SIZE)
        {

            if (map.tile[mapY][mapX] != 0)
            {
                /*A MODIFIER*/
                if (map.tile[mapY][mapX] == 109)
                {
                    
                    initializeMonster(mapX * TILE_SIZE, mapY * TILE_SIZE);
                    if (mapY<10)
                    {
                        map.tile[mapY][mapX] = 0;
                    }
                    else
                    map.tile[mapY][mapX] = 235;

                }
                if (map.tile[mapY][mapX] == 50)
                {
                    initializeButin(mapX * TILE_SIZE, mapY * TILE_SIZE);
                    
                    map.tile[mapY][mapX] = 0;

                }
                if (map.tile[mapY][mapX] == 61)
                {
                    NextLevel(mapX * TILE_SIZE, mapY * TILE_SIZE);
                    
                   

                }

                
            }

           

            a = map.tile[mapY][mapX];

            

            ysource = a / 60* TILE_SIZE;

            

            xsource = a % 60 * TILE_SIZE;

            

            drawTile(map.tileSet, x, y, xsource, ysource);

            mapX++;
        }

        mapY++;
    }
  }  


   void mapCollision(Hero *entity)
 {

    int i, x1, x2, y1, y2,j;
    int solide [MAX_BLANK_TILE]= BLANK_TILE;

    entity->onGround = 0;

   
   
    if(entity->h > TILE_SIZE)
        i = TILE_SIZE;
    else
        i = entity->h;

    

    for (;;)
    {
        
        

        x1 = (entity->x + entity->dirX) / TILE_SIZE;
        x2 = (entity->x + entity->dirX + entity->w -1) / TILE_SIZE;

       

        y1 = (entity->y) / TILE_SIZE;
        y2 = (entity->y + i - 1) / TILE_SIZE;

        

        if (x1 >= 0 && x2 < MAX_MAP_X && y1 >= 0 && y2 < MAX_MAP_Y)
        {
           

            if (entity->dirX > 0)
            {

               for (j=0; j<MAX_BLANK_TILE; j++)
               {



                if (map.tile[y1][x2] == solide[j] || map.tile[y2][x2] == solide[j])
                {
                    

                    entity->x = x2 * TILE_SIZE;

                    entity->x -= entity->w + 1;

                    entity->dirX = 0;

                }
                }

            }

           

            else if (entity->dirX < 0)
            {
                for (j=0; j<MAX_BLANK_TILE; j++)
               {


                if (map.tile[y1][x1] == solide[j] || map.tile[y2][x1] == solide[j])
                {

                    entity->x = (x1 + 1) * TILE_SIZE;

                    entity->dirX = 0;
                }

            }
            }

        }

        
        if (i == entity->h)
        {
            break;
        }

        
        i += TILE_SIZE;

        if (i > entity->h)
        {
            i = entity->h;
        }
    }

    
    if(entity->w > TILE_SIZE)
        i = TILE_SIZE;
    else
        i = entity->w;


    for (;;)
    {
        x1 = (entity->x) / TILE_SIZE;
        x2 = (entity->x + i) / TILE_SIZE;

        y1 = (entity->y + entity->dirY) / TILE_SIZE;
        y2 = (entity->y + entity->dirY + entity->h) / TILE_SIZE;

        if (x1 >= 0 && x2 < MAX_MAP_X && y1 >= 0 && y2 < MAX_MAP_Y)
        {
            if (entity->dirY > 0)
            {
                for (j=0; j<MAX_BLANK_TILE; j++)
               {
               
                if (map.tile[y2][x1] == solide[j] || map.tile[y2][x2] == solide[j])
                {
                    
                    entity->y = y2 * TILE_SIZE;
                    entity->y -= entity->h;

                    entity->dirY = 0;

                    entity->onGround = 1;
                }
            }

            }

            else if (entity->dirY < 0)
            {

               for (j=0; j<MAX_BLANK_TILE; j++)
               {

                if (map.tile[y1][x1] == solide[j] || map.tile[y1][x2] == solide[j])
                {

                    entity->y = (y1 + 1) * TILE_SIZE;

                    entity->dirY = 0;

                }
            }

            }
        }

        
        if (i == entity->w)
        {
            break;
        }

        i += TILE_SIZE;

        if (i > entity->w)
        {
            i = entity->w;
        }
    }

   
    entity->x += entity->dirX;
    entity->y += entity->dirY;

    
    if (entity->x < 0)
    {
        entity->x = 0;
    }

    else if (entity->x + entity->w >= map.maxX)
    {
        entity->x = map.maxX - entity->w - 1;
    }

    
    if (entity->y > map.maxY)
    {
        entity->timerMort = 180;
        playSoundFx(MORT);
    }
}


 void monstreCollision(GameObject *entity)
 {

    int i, x1, x2, y1, y2,j;
    int solide [MAX_BLANK_TILE]= BLANK_TILE;

    entity->onGround = 0;

   
   
    if(entity->h > TILE_SIZE)
        i = TILE_SIZE;
    else
        i = entity->h;

    

    for (;;)
    {
        
        

        x1 = (entity->x + entity->dirX) / TILE_SIZE;
        x2 = (entity->x + entity->dirX + entity->w -1) / TILE_SIZE;

       

        y1 = (entity->y) / TILE_SIZE;
        y2 = (entity->y + i - 1) / TILE_SIZE;

        

        if (x1 >= 0 && x2 < MAX_MAP_X && y1 >= 0 && y2 < MAX_MAP_Y)
        {
           

            if (entity->dirX > 0)
            {

               for (j=0; j<MAX_BLANK_TILE; j++)
               {



                if (map.tile[y1][x2] == solide[j] || map.tile[y2][x2] == solide[j])
                {
                    

                    entity->x = x2 * TILE_SIZE;

                    entity->x -= entity->w + 1;

                    entity->dirX = 0;

                }
                }

            }

           

            else if (entity->dirX < 0)
            {
                for (j=0; j<MAX_BLANK_TILE; j++)
               {


                if (map.tile[y1][x1] == solide[j] || map.tile[y2][x1] == solide[j])
                {

                    entity->x = (x1 + 1) * TILE_SIZE;

                    entity->dirX = 0;
                }

            }
            }

        }

        
        if (i == entity->h)
        {
            break;
        }

        
        i += TILE_SIZE;

        if (i > entity->h)
        {
            i = entity->h;
        }
    }

    
    if(entity->w > TILE_SIZE)
        i = TILE_SIZE;
    else
        i = entity->w;


    for (;;)
    {
        x1 = (entity->x) / TILE_SIZE;
        x2 = (entity->x + i) / TILE_SIZE;

        y1 = (entity->y + entity->dirY) / TILE_SIZE;
        y2 = (entity->y + entity->dirY + entity->h) / TILE_SIZE;

        if (x1 >= 0 && x2 < MAX_MAP_X && y1 >= 0 && y2 < MAX_MAP_Y)
        {
            if (entity->dirY > 0)
            {
                for (j=0; j<MAX_BLANK_TILE; j++)
               {
               
                if (map.tile[y2][x1] == solide[j] || map.tile[y2][x2] == solide[j])
                {
                    
                    entity->y = y2 * TILE_SIZE;
                    entity->y -= entity->h;

                    entity->dirY = 0;

                    entity->onGround = 1;
                }
            }

            }

            else if (entity->dirY < 0)
            {

               for (j=0; j<MAX_BLANK_TILE; j++)
               {

                if (map.tile[y1][x1] == solide[j] || map.tile[y1][x2] == solide[j])
                {

                    entity->y = (y1 + 1) * TILE_SIZE;

                    entity->dirY = 0;

                }
            }

            }
        }

        
        if (i == entity->w)
        {
            break;
        }

        i += TILE_SIZE;

        if (i > entity->w)
        {
            i = entity->w;
        }
    }

   
    entity->x += entity->dirX;
    entity->y += entity->dirY;

    
    if (entity->x < 0)
    {
        entity->x = 0;
    }

    else if (entity->x + entity->w >= map.maxX)
    {
        entity->x = map.maxX - entity->w - 1;
    }

    
    if (entity->y > map.maxY)
    {
        entity->timerMort = 60;
    }
}







void drawStartMenu(int maxW, int maxH)
{

    char text[200];

    sprintf(text, "ESPRIT GAIDEN");
    drawStringBlanc(text, (maxW-200)/2, 50, font);

    sprintf(text, "GAME OVER");
    drawStringBlanc(text, (maxW-150)/2, 150, font);

    if(jeu.choice != 0)
    {
        sprintf(text, "RESTART");
        drawStringBlanc(text, (maxW-100)/2, 300, font);
    }
    if(jeu.choice != 1)
    {
        sprintf(text, "QUIT");
        drawStringBlanc(text, (maxW-100)/2, 380, font);
    }


    if(jeu.choice == 0)
    {
        sprintf(text, "> RESTART");
        drawStringBlanc(text, (maxW-50)/2, 300, font);
    }
    else if(jeu.choice == 1)
    {
        sprintf(text, "> QUIT");
        drawStringBlanc(text, (maxW-50)/2, 380, font);
    }


    sprintf(text, "Step By Step");
    drawStringBlanc(text, maxW-300, maxH-150, font);


}